﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using grupo2DEWStudio7i.Dominio;
using grupo2DEWStudio7i.Negocio;

namespace grupo2DEWStudio7i.Controllers
{
    public class ReservaController : Controller
    {
        //Dependencias
        public AdministracionServiceImpl AdminService { get; set; }

        //
        // GET: /Reserva/

        public ActionResult Index()
        {
            ICollection<Sala> modelo = AdminService.ListarSalas();
            TempData["locales"] = ListadoLocales();
            return View(modelo);
        }

        [HttpPost]
        public ActionResult Buscar(FormCollection formulario)
        {
            ICollection<Sala> modelo = new List<Sala>();

            try
            {
                String sNombreSala = formulario["NombreSala"].ToString();
                String sInstrumento = formulario["Instrumento"].ToString();
                String sServicio = formulario["Servicio"].ToString();

                int? iCantInst = null;

                if (!String.IsNullOrEmpty(formulario["CantInst"]))
                    iCantInst = int.Parse(formulario["CantInst"].ToString());

                int? iCapacidad = null;

                if (!String.IsNullOrEmpty(formulario["Capacidad"]))
                    iCapacidad = int.Parse(formulario["Capacidad"].ToString());

                int? iLocalCodigo = null;

                if (!String.IsNullOrEmpty(formulario["LocalCodigo"]))
                    iLocalCodigo = int.Parse(formulario["LocalCodigo"].ToString());

                // TODO: Add insert logic here

                modelo = AdminService.ListarXBusquedaReserva(sNombreSala, iLocalCodigo, iCantInst, sInstrumento, sServicio, iCapacidad);

                TempData["locales"] = ListadoLocales();


            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);

                TempData["locales"] = ListadoLocales();

            }

            return View("Index", modelo);
        }

        public ActionResult Instrumentos(int idSala)
        {
            ICollection<Instrumento> modelo = AdminService.ListarInstrumentosXSala(idSala);
            return View(modelo);
        }

        public ActionResult Servicios(int idSala)
        {
            ICollection<Servicio> modelo = AdminService.ListarServiciosXSala(idSala);
            return View(modelo);
        }

        private ICollection<SelectListItem> ListadoLocales()
        {
            ICollection<SelectListItem> locales = new List<SelectListItem>();
            locales.Add(new SelectListItem() { Value = string.Empty, Text = "Seleccione.." });
            foreach (Local local in AdminService.ListarLocales())
                locales.Add(new SelectListItem()
                {
                    Value = local.Codigo.ToString(),
                    Text = local.Nombre
                });
            return locales;
        }


        //
        // GET: /Reserva/Create

        public ActionResult Reservar(int idSala)
        {
            TempData["idSala"] = idSala;

            return View();
        }

        //
        // POST: /Reserva/Create

        [HttpPost]
        public ActionResult Reservar(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                Int32 idSala = Int32.Parse(TempData["idSala"].ToString());

                TempData["idSala"] = idSala;

                Reserva oReserva = AdminService.Reservar(2, idSala, collection["Dia"].ToString(), collection["Hora"].ToString(), Decimal.Parse(collection["CostoTotal"].ToString()), Int32.Parse(collection["Numero"].ToString()));

                TempData["idReserva"] = oReserva.Codigo;

                List<DetalleReserva> modelo = new List<DetalleReserva>();

                TempData["Servicios"] = ListadoServicio();
                TempData["Instrumentos"] = ListadoInstrumento();

                return View("ReservarDetalle", modelo);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, "Error al reservar: " + ex.Message);

                Reserva Reserva = new Reserva()
                {
                    Dia = collection["Dia"],
                    Hora = collection["Hora"],
                    Numero = Int32.Parse(collection["Numero"].ToString()),
                    CostoTotal = Decimal.Parse(collection["CostoTotal"].ToString())
                };

                return View(Reserva);
            }
        }


        public ActionResult ReservarDetalle()
        {
            Int32 idSala = Int32.Parse(TempData["idSala"].ToString());
            TempData["idSala"] = idSala;

            Int32 idReserva = Int32.Parse(TempData["idReserva"].ToString());
            TempData["idReserva"] = idSala;

            TempData["Servicios"] = ListadoServicio();
            TempData["Instrumentos"] = ListadoInstrumento();

            return View();
        }

        [HttpPost]
        public ActionResult AddServicio(FormCollection formulario)
        {
            Int32 idReserva = Int32.Parse(TempData["idReserva"].ToString());
            TempData["idReserva"] = idReserva;

            TempData["Servicios"] = ListadoServicio();
            TempData["Instrumentos"] = ListadoInstrumento();

            int? icodigoservicio = null;

            if (!String.IsNullOrEmpty(formulario["codigoservicio"]))
                icodigoservicio = int.Parse(formulario["codigoservicio"].ToString());

            AdminService.ReservarDetalle(idReserva, 0, null, icodigoservicio);

            ICollection<DetalleReserva> modelo = AdminService.ListarReservarDetalle(idReserva);

            return View("ReservarDetalle", modelo);
        }

        [HttpPost]
        public ActionResult AddInstrumento(FormCollection formulario)
        {
            Int32 idReserva = Int32.Parse(TempData["idReserva"].ToString());
            TempData["idReserva"] = idReserva;

            TempData["Servicios"] = ListadoServicio();
            TempData["Instrumentos"] = ListadoInstrumento();

            int? iCodInstrumento = null;

            if (!String.IsNullOrEmpty(formulario["codigoinstrumento"]))
                iCodInstrumento = int.Parse(formulario["codigoinstrumento"].ToString());

            AdminService.ReservarDetalle(idReserva, 0, iCodInstrumento, null);

            ICollection<DetalleReserva> modelo = AdminService.ListarReservarDetalle(idReserva);

            return View("ReservarDetalle", modelo);
        }


        private ICollection<SelectListItem> ListadoServicio()
        {
            Int32 idSala = Int32.Parse(TempData["idSala"].ToString());
            TempData["idSala"] = idSala;

            ICollection<SelectListItem> servicios = new List<SelectListItem>(idSala);
            servicios.Add(new SelectListItem() { Value = string.Empty, Text = "Seleccione.." });
            foreach (Servicio servicio in AdminService.ListarServiciosXSala(idSala))
                servicios.Add(new SelectListItem()
                {
                    Value = servicio.Codigo.ToString(),
                    Text = servicio.Descripcion
                });
            return servicios;
        }

        private ICollection<SelectListItem> ListadoInstrumento()
        {
            Int32 idSala = Int32.Parse(TempData["idSala"].ToString());
            TempData["idSala"] = idSala;

            ICollection<SelectListItem> instrumentos = new List<SelectListItem>();
            instrumentos.Add(new SelectListItem() { Value = string.Empty, Text = "Seleccione.." });
            foreach (Instrumento instrumento in AdminService.ListarInstrumentosXSala(idSala))
                instrumentos.Add(new SelectListItem()
                {
                    Value = instrumento.CodInstrumento.ToString(),
                    Text = instrumento.TipoInstru
                });
            return instrumentos;
        }


    }
}